package com.szyjjk.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.szyjjk.entity.*;
import com.szyjjk.service.*;
import com.szyjjk.util.DateUtil;
//import com.szyjjk.util.GeoTool;
import com.szyjjk.util.GeoTool;
import com.szyjjk.util.UUIDUtils;
import org.apache.commons.lang3.StringUtils;
//import org.locationtech.jts.io.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.*;

import static com.szyjjk.constants.SzyjjkConstant.SBW_MAP_LOOK;

/**
 * @program: szyjjk
 * @description: 针对Dm3数据站的控制层
 * @author: yangyue
 * @create: 2020-09-23 16:10
 */
@Controller
@RequestMapping("/dm3")
public class Dm3Controller {


    @Autowired
    private RunFilePathrService runFilePathrService;
    @Autowired
    private SysHearWordService sysHearWordService;
    @Autowired
    private RunPropertyServices runPropertyServices;
    @Autowired
    private RunMapDataService runMapDataService;
    @Autowired
    private RunMapPicService runMapPicService;


    /**
     * @return java.util.List<com.szyjjk.entity.RunFilePath>
     * @Author yangyue
     * @Description //TODO
     * @Date 9:52 2020/9/28
     * @Param []
     **/
    @RequestMapping(value = "/findMapList.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<RunFilePath> findMapList() {
        List<RunFilePath> findFilePathByHz = runFilePathrService.findFilePathByHz("map");
        return findFilePathByHz;
    }

    /**
     * @return com.szyjjk.entity.RunFilePath
     * @Author yangyue
     * @Description //TODO 根据名字查询
     * @Date 16:36 2020/9/28
     * @Param [mapName]
     **/
    @RequestMapping(value = "/getMapTime.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RunFilePath getMapTime(String mapName) {
        return runFilePathrService.findById(mapName);
    }


    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 根据 type（0 实测 1 模拟） mapName map名字  时间   和属性名 查询或者创建对应的shp数据
     * @Date 10:52 2020/9/28
     * @Param [type, mapName, timestamp, proName]
     **/
    @RequestMapping(value = "/findShpData.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject findShpData(int type, String mapName, long timestamp, String proName, String tir, int ColorType) {
        //首先解析时间戳 取对应一个小时的时间[]
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(timestamp));
        calendar.set(Calendar.SECOND, 0);
        List<RunMapData> rusultData = new ArrayList<>();
        JSONObject result = new JSONObject();
        String tableName = "";
        switch (tir) {
            case "第一层":
            case "First floor":
                tableName = "run_map_data_1";
                break;
            case "第二层":
            case "Second floor":
                tableName = "run_map_data_2";
                break;
            case "第三层":
            case "Third layer":
                tableName = "run_map_data_3";
                break;
            case "第四层":
            case "Fourth floor":
                tableName = "run_map_data_4";
                break;
            case "第五层":
            case "Fifth floor":
                tableName = "run_map_data_5";
                break;
            default:
                break;
        }
        if (type == 0) {
            calendar.set(Calendar.MINUTE, 30);
            Date begtime = calendar.getTime();
            //实测数据，实测数据主要是根据时间，查询对应数据的一个小时内的数据,然后返回对应的值
            List<SysHardWareData> sysHardWareData = this.sysHearWordService.finProByTimeName(UUIDUtils.gethardWareData(proName), begtime);
            RunMapData runMapData = new RunMapData();
            if (sysHardWareData.size() > 0) {
                SysHardWareData sysHardWareData1 = sysHardWareData.get(0);
                runMapData.setMapSiteXy("113.9615019,22.47633");
                runMapData.setRemake(sysHardWareData1.getDataValue());
                if (StringUtils.isNotEmpty(sysHardWareData1.getDataRemark())) {
                    tir = sysHardWareData1.getDataRemark();
                } else {
                    tir = "无";
                }
            } else {
                return null;
            }
            // 判断时间
            rusultData.add(runMapData);
            RunProperty runProperty = this.runPropertyServices.findByProName(proName);
            result = GeoTool.createGeoJSONLayerMap(rusultData, tir, proName, runProperty, type, ColorType, null);
        } else if (type == 1) {
            calendar.set(Calendar.MINUTE, 0);
            Date begtime = calendar.getTime();
            //通过 属性名称 文件ID,时间，层数 查询 RunMapData
            rusultData = runMapDataService.findMapData(proName, mapName, begtime, tableName);
            RunProperty runProperty = this.runPropertyServices.findByProName(proName);
            //在去重排序查询一次
            if (ColorType == 1) {
                result = GeoTool.createGeoJSONLayerMap(rusultData, tir, proName, runProperty, type, ColorType, runMapDataService.findMapDateDistinct(proName, mapName, begtime, tableName));
            } else {
                result = GeoTool.createGeoJSONLayerMap(rusultData, tir, proName, runProperty, type, ColorType, null);
            }

        } else if (type == 2) {
            //查找图片
            calendar.set(Calendar.MINUTE, 0);
            Date begtime = calendar.getTime();
            List<RunMapPic> picList = runMapPicService.findByNameTimePro(mapName, proName, begtime);
            //只需要也只可能是一个
            if (picList.size() > 0) {
                result.put("path", picList.get(0).getMapPicPath());
                result.put("picName", picList.get(0).getMapPicName());
            }
        } else if (type == 3) {
            //获取最大最小值
            calendar.set(Calendar.MINUTE, 0);
            Date begtime = calendar.getTime();
            if (!StringUtils.isNotEmpty(mapName)) {
                return null;
            }
            rusultData = runMapDataService.findMapDateDistinct(proName, mapName, begtime, tableName);
            if (rusultData.size() > 0) {
                if(rusultData.get(0)==null){
                    result.put("min", "0");
                }else{
                    result.put("min", Double.parseDouble(rusultData.get(0).getRemake()));
                }
                if(rusultData.get(rusultData.size() - 1)==null){
                    result.put("max", "0");
                }else{
                    result.put("max", Double.parseDouble(rusultData.get(rusultData.size() - 1).getRemake()));
                }

            }
        } else if (type == 4) {
            calendar.set(Calendar.MINUTE, 0);
            Date begtime = calendar.getTime();
            //通过 属性名称 文件ID,时间，层数 查询 RunMapData
            rusultData = runMapDataService.findMapDateDistinct(proName, mapName, begtime, tableName);
            //移除.oooo
            for (int i = 0; i < rusultData.size(); i++) {
                if(rusultData.get(i)==null){
                    rusultData.remove(i);
                }else{
                    rusultData.get(i).setRemake(Double.parseDouble(rusultData.get(i).getRemake())+"");
                }
            }
//            for (RunMapData rusultDatum : rusultData) {
//                if(rusultDatum==null){
//                    continue;
//                }else{
//
//                }
//
//            }
            int number=rusultData.size()/5;
            JSONArray jsonArray=new JSONArray();
            if (rusultData.size() > 0) {
                if(rusultData.size()<5){
                    if(rusultData.size()>0){
                        JSONObject proJSONblue = new JSONObject();
                        proJSONblue.put("color", "blue");
                        proJSONblue.put("value",  "[" + rusultData.get(0).getRemake() + "-" + rusultData.get(0).getRemake() + "]");
                        jsonArray.add(proJSONblue);
                        if(rusultData.size()>1){
                            JSONObject proJSONgreen = new JSONObject();
                            proJSONgreen.put("color", "green");
                            proJSONgreen.put("value",  "[" + rusultData.get(0).getRemake() + "-" + rusultData.get(1).getRemake() + "]");
                            jsonArray.add(proJSONgreen);
                            if(rusultData.size()>2){
                                JSONObject proJSONyellow = new JSONObject();
                                proJSONyellow.put("color", "yellow");
                                proJSONyellow.put("value",  "[" + rusultData.get(1).getRemake() + "-" + rusultData.get(2).getRemake() + "]");
                                jsonArray.add(proJSONyellow);
                                if(rusultData.size()>3){
                                    JSONObject proJSONorange = new JSONObject();
                                    proJSONorange.put("color", "orange");
                                    proJSONorange.put("value",  "[" + rusultData.get(2).getRemake() + "-" + rusultData.get(3).getRemake() + "]");
                                    jsonArray.add(proJSONorange);
                                    if(rusultData.size()>4){
                                        JSONObject proJSONred = new JSONObject();
                                        proJSONred.put("color", "red");
                                        proJSONred.put("value", "[" + rusultData.get(3).getRemake() + "-" + rusultData.get(4).getRemake() + "]");
                                        jsonArray.add(proJSONred);
                                    }
                                }
                            }
                        }
                    }
                }else{
                    JSONObject proJSONblue = new JSONObject();
                    proJSONblue.put("color", "blue");
                    proJSONblue.put("value",  "[" + rusultData.get(0).getRemake() + "-" + rusultData.get(number).getRemake() + "]");
                    jsonArray.add(proJSONblue);

                    JSONObject proJSONgreen = new JSONObject();
                    proJSONgreen.put("color", "green");
                    proJSONgreen.put("value",  "[" + rusultData.get(number).getRemake() + "-" + rusultData.get(number*2).getRemake() + "]");
                    jsonArray.add(proJSONgreen);

                    JSONObject proJSONyellow = new JSONObject();
                    proJSONyellow.put("color", "yellow");
                    proJSONyellow.put("value",  "[" + rusultData.get(number*2).getRemake() + "-" + rusultData.get(number*3).getRemake() + "]");
                    jsonArray.add(proJSONyellow);

                    JSONObject proJSONorange = new JSONObject();
                    proJSONorange.put("color", "orange");
                    proJSONorange.put("value",  "[" + rusultData.get(number*3).getRemake() + "-" + rusultData.get(number*4).getRemake() + "]");
                    jsonArray.add(proJSONorange);

                    JSONObject proJSONred = new JSONObject();
                    proJSONred.put("color", "red");
                    proJSONred.put("value", "[" + rusultData.get(number*4).getRemake() + "-" + rusultData.get(rusultData.size()-1).getRemake() + "]");
                    jsonArray.add(proJSONred);
                }





                result.put("tuli",jsonArray);
            }
        }
        return result;
    }

    /**
     * @return com.alibaba.fastjson.JSONObject
     * @Author yangyue
     * @Description //TODO 获得图裂
     * @Date 14:27 2020/11/3
     * @Param [proName]
     **/
    @RequestMapping(value = "/getLegendPro.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONArray getLegendPro(String proName) {
        RunProperty runProperty = this.runPropertyServices.findByProName(proName);
        JSONArray jsonArray = new JSONArray();
        //计算红色
        if (StringUtils.isNotEmpty(runProperty.getProRed())) {
            String[] spileRed = runProperty.getProRed().split("&");
            for (String red : spileRed) {
                JSONObject proJSON = new JSONObject();
                proJSON.put("color", "red");
                proJSON.put("value", red);
                jsonArray.add(proJSON);
            }
        }

        if (StringUtils.isNotEmpty(runProperty.getProYellow())) {
            String[] spileYellow = runProperty.getProYellow().split("&");
            for (String yellow : spileYellow) {
                JSONObject proJSON = new JSONObject();
                proJSON.put("color", "yellow");
                proJSON.put("value", yellow);
                jsonArray.add(proJSON);
            }
        }
        if (StringUtils.isNotEmpty(runProperty.getProGreen())) {
            String[] spileGreen = runProperty.getProGreen().split("&");
            for (String Green : spileGreen) {
                JSONObject proJSON = new JSONObject();
                proJSON.put("color", "Green");
                proJSON.put("value", Green);
                jsonArray.add(proJSON);
            }
        }

        return jsonArray;
    }


    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO
     * @Date 12:01 2020/9/24
     * @Param [response]
     **/
    @RequestMapping(value = "/homeDm3.do")
    public String homeDm3(HttpServletResponse response) {

        return "home/homeDm3Show";
    }

    @RequestMapping(value = "/test.do")
    public String test(HttpServletResponse response) {

        return "home/test";
    }


}